import dispatch
from genshi.template.plugin import ET

from mnemoply._renewed_utils import find_class, get_by_key_and_delete
import mnemoply._renewed_lib.widgets as widgets
import mnemoply._renewed_lib.validators as validators
from mnemoply._renewed_constants import today, day_captions, getDefaultController
from mnemoply._renewed_model import columns as cols

fk_for_auto_complete = ['Account','Personality', 'Group']

@dispatch.generic()
def column_item_view(column, value):
    pass


@column_item_view.when("isinstance(column, cols.SOBoolCol)")
def column_item_view_boolean_col(column, value):
    if value is None:
        return _("*No value*")
    return value and _('Yes') or _('No')



@column_item_view.when("isinstance(column, cols.SOForeignKey)\
                    and column.foreignKey == 'Personality'\
                    and column.name=='studentID'")
def column_item_view_fkey_col(column, value):
    if value is None:
        return _("*No value*")
    fkey_class = find_class('StudentNew')
    
    obj = fkey_class.get(int(value))
    title = obj.title
    return  widgets.FKeyLink(fkey_class='StudentNew',
                             obj_id=int(value),
                             obj_title=title)

@column_item_view.when("isinstance(column, cols.SOForeignKey)")
def column_item_view_fkey_col(column, value):
    if value is None:
        return _("*No value*")
    fkey_class = find_class(column.foreignKey)
    obj = fkey_class.get(int(value))
    title = obj.title
    return  widgets.FKeyLink(fkey_class=column.foreignKey,
                             obj_id=int(value),
                             obj_title=title)

@column_item_view.when("isinstance(column, cols.SODateCol)")
def column_item_view_date_col(column, value):
    if value is None:
        return _("*No value*")
    return value.strftime("%d.%m.%Y")


@column_item_view.when("isinstance(column, cols.SOTimeCol)")
def column_item_view_date_col(column, value):
    if value is None:
        return _("*No value*")
    return '%02d:%02d' % (value.hour, value.minute)


@column_item_view.when("column.name.lower().find('email') != -1")
def  column_item_view_email_col(column, value):
    if value is None:
        return _("*No value*")
    return widgets.URLLink(link = "mailto:%s"%value, text = value)


@column_item_view.when(dispatch.strategy.default)
def column_item_view_generic_col(column, value):
    if value is None:
        return _("*No value*")
    return str(value)


@dispatch.generic()
def column_widget(column, **params):
    pass


@column_widget.when("isinstance(column, cols.SOCol)")
def column_widget_generic_col(column, **params):
    if hasattr(column, 'length') and column.length:
        return widgets.TextField(**params)
    else:
        return widgets.TextArea(rows=7, cols=50,**params)


@column_widget.when("isinstance(column, cols.SOForeignKey)")
def column_widget_fkey_col(column, **params):
    name = column.foreignKey 
    if name in fk_for_auto_complete:
        return widgets.AutoCompleteField(name,**params)
    else :
        fkey_class = find_class(name)
        select = fkey_class.select()
        return widgets.DictionaryField(select,**params)


@column_widget.when("isinstance(column, cols.SOForeignKey)\
                    and column.foreignKey == 'Personality'\
                    and column.name=='studentID'")
def column_widget_student_col(column, **params):
    return widgets.StudentField(**params)


#@column_widget.when("isinstance(column, cols.SOForeignKey) \
#                    and column.foreignKey == 'Discont'")
#def column_widget_discont_col(column, **params):
#    x = column_parms(column)
#    return widgets.getDicontDictionary()

@column_widget.when("isinstance(column,cols.SODateCol)")
def column_widget_date_col(column, **params):
    return widgets.CalendarDatePicker(**params)


@column_widget.when("isinstance(column, cols.SOIntCol)")
def column_widget_int_col(column, **params):
    validator = validators.tg_validators.Int()
    return widgets.TextField(validator = validator,**params)


@column_widget.when("isinstance(column, cols.SOIntCol)\
                    and column.name=='school_form'")
def column_widget_school_form_col(column, **params):
    validator = validators.tg_validators.Int(min=0,max=12)
    return widgets.TextField(validator = validator,**params)


@column_widget.when("isinstance(column, cols.SOUnicodeCol)\
                    and column.name=='phone_main'")
def column_widget_phone_col(column, **params):
    return widgets.PhoneMainField( **params)


@column_widget.when("isinstance(column, cols.SOUnicodeCol)\
                    and column.name=='prefered_day'")
def column_widget_phone_col(column, **params):
    return widgets.PreferedDayField( **params)

